package com.adingxiong.cft.quartz;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName DuplicateDataJobDemo
 * @Description  定时任务中重复赋值  情况中出现的结果  遵循map的存值原理，存在重复key的 value会被覆盖更新，没有的合并
 * @Author xiongchao
 * @Date 2021/1/11 15:26
 **/
@Slf4j
public class DuplicateDataJobDemo {


    public static void main(String[] args) throws SchedulerException, InterruptedException {
        JobDetail jobDetail = JobBuilder.newJob(DuplicateData.class)
                .withIdentity("duplicate","group01")
                .usingJobData("name","wangqiang")
                .build();
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("duplicate_group")
                .usingJobData("name","lisi")
                .usingJobData("age","26")
                .startNow()
                .build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.scheduleJob(jobDetail,trigger);
        scheduler.start();
        Thread.sleep(2_100);
        scheduler.shutdown();
    }



    public static class DuplicateData implements Job {

        private String name ;

        private String age;

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(String age) {
            this.age = age;
        }

        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            //默认是自动覆盖重复的key ，如果想获取之前的值  jobdetail中的值
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
            List<Map.Entry<String, Object>> collect = mergedJobDataMap.entrySet().stream().filter(e -> jobDataMap.getWrappedMap().containsKey(e.getKey()))
                    .collect(Collectors.toList());

            log.info(collect.toString());
            log.info("===============================================");
            log.info("hi , my name is {}, i am {} year old" ,name ,age);

            log.info("===============================================");
        }


    }

}
